package com.ybkj.daijia.server.service.impl;

import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.impl.PageImpl;
import com.ybkj.daijia.page.impl.PageRequest;
import com.ybkj.daijia.server.mapper.BrandMapper;
import com.ybkj.daijia.server.mapper.BrandTypeMapper;
import com.ybkj.daijia.server.mapper.CarMapper;
import com.ybkj.daijia.server.model.Brand;
import com.ybkj.daijia.server.model.BrandType;
import com.ybkj.daijia.server.service.BrandService;
import java.io.File;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @author Shine
 */
@Service
public class DefaultBrandService implements BrandService {

    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private BrandTypeMapper brandTypeMapper;
    @Autowired
    private CarMapper carMapper;

    @Override
    public int save(Brand brand) {
        int i = brandMapper.insertBrand(brand);
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id, HttpServletRequest req) {
        String path = req.getSession().getServletContext().getRealPath("/");
        Brand brand = brandMapper.selectById(id);
        String oldPath = path + brand.getPhoto();
        File oldFile = new File(oldPath);
        boolean delete = oldFile.delete();
        if (delete) {
        } else {
        }
        carMapper.deleteBrandId(id); // 删除关联汽车的品牌
        List<BrandType> list = brandTypeMapper.selectByBrandId(brand.getId());
        if (!CollectionUtils.isEmpty(list)) {
            for (BrandType brandType : list) {
                carMapper.deleteBrandTypeId(brandType.getId());
                brandTypeMapper.deleteById(brandType.getId());
            }
        }
        int i = brandMapper.deleteById(id);
        return i;
    }

    @Override
    public int update(Brand brand) {
        int i = brandMapper.updateById(brand);
        return i;

    }

    @Override
    public Brand findById(Long id) {
        Brand brand = brandMapper.selectById(id);

        return brand;
    }

    @Override
    public Brand findByName(String name) {
        Brand brand = brandMapper.selectByName(name);

        return brand;

    }

    @Override
    public Brand findOnlyByName(String name) {
        Brand brand = brandMapper.selectOnlyByName(name);

        return brand;

    }

    @Override
    public List<Brand> findAll() {
        List<Brand> list = brandMapper.selectAll();

        return list;
    }

    @Override
    public Page<Brand> queryAll() {

        Pageable pageable = new PageRequest(Pager.getPage(), Pager.getLimit());

        Map<String, Object> params = new HashMap<String, Object>();
//		params.put("offset", Pager.getOffset());
//		params.put("size", Pager.getLimit());

        long total = brandMapper.countByPage(params);
        List<Brand> content = brandMapper.selectByPage(params);

        if (!CollectionUtils.isEmpty(content)) {
            for (Brand brand : content) {
                List<BrandType> brandTypes = brandTypeMapper.selectByBrandId(brand.getId());
                if (!CollectionUtils.isEmpty(brandTypes)) {
                    Collections.sort(brandTypes, new Comparator<BrandType>() {
                        public int compare(BrandType arg0, BrandType arg1) {
                            return arg0.getSequence().compareTo(arg1.getSequence());
                        }
                    });
                    brand.setBrandTypes(brandTypes);
                }
            }
        }

        return new PageImpl<Brand>(content, pageable, total);

    }

    @Override
    public List<Brand> findByBrand(String brandName) {
        List<Brand> list = brandMapper.selectByBrand("%" + brandName + "%");

        return list;
    }

    @Override
    public Brand findByBrandId(Long id) {

        return brandMapper.selectByBrandId(id);
    }

    @Override
    public List<Brand> listWithName() {

        return brandMapper.selectWithName();
    }

}